import { createSerialDates } from "@src/dataFormatConversion/tool";

import { DomMessageInstance } from "../../utils/DomMessage";
import { OverallQueryDataConfig } from "../type";
import { DataSourceTypeEnum } from "@src/globalData/enum";

/** 综合查询系统中需要手动获取的数据项 */
const getDataInfo:OverallQueryDataConfig[] = [
    /** 市场运营 */
    {
      statement: `【信息披露】-【综合查询】-【市场运营】- 【交易组织及出清】-【现货市场申报及出清信息】-【日前市场出清电量】-【日前各类电源出清电量和台数】`,
      pageName: '日前各类电源出清电量和台数',
      cols: [2,3,4,5,6,7,8,9,10,11,12,13,14,15],
      typeKey: DataSourceTypeEnum.AHEAD_POWER,
      keys: ['directEle','directNum','nuclearEle','nuclearNum','newEnergyEle','newEnergyNum','localEle','localNum','indepEle','indepNum','pumpEle','pumpNum','virtualEle','virtualNum']
    },
    {
      statement: `【市场运营】- 【交易组织及出清】-【现货市场申报及出清信息】-【实时市场出清电量】-【实时各类电源出清电量和台数】`,
      pageName: '实时各类电源出清电量和台数',
      cols: [2,3,4,5,6,7,8,9,10,11,12,13,14,15],
      typeKey: DataSourceTypeEnum.ACT_POWER,
      keys: ['directEle','directNum','nuclearEle','nuclearNum','newEnergyEle','newEnergyNum','localEle','localNum','indepEle','indepNum','pumpEle','pumpNum','virtualEle','virtualNum']
    }
]

/** 点击页面中 获取数据 按钮时触发的获取dom中数据的方法 */
export const getCurrentPageInfo = ():Partial<OverallQueryDataConfig> | void | undefined => {
  let data:string[][] = []

  const dom = document?.querySelector('iframe')?.contentDocument || document
  const _pageName = dom.querySelector('.rows-height-counter')?.querySelector('td')?.textContent
  if(!_pageName) return DomMessageInstance.error({content:'获取页面信息失败'})
 
  
  const dataInfo = getDataInfo.find(item => item.pageName === _pageName)
  if(!dataInfo) return DomMessageInstance.error({content:'当前页面未配置获取数据'})
  if(dataInfo?.pageValidate) {
    const {validateResult = false, errMsg = ''} = dataInfo.pageValidate()
    if(!validateResult) return DomMessageInstance.error({content: errMsg})
  }

  
  const {statement, pageName, cols} = dataInfo

  for(let i = 0; i < cols.length; i++) {
    const list = Array.from(dom?.querySelector('.rows-height-counter')?.querySelectorAll?.(`tr > td:nth-child(${cols[i]})`) || {length:0});
    const colData = list.slice(list.length-96).map(val => val.innerHTML)
    data.push(colData)
  }
  if(!data?.[0].length) {
    const message = `获取数据失败! <br> ${statement} <br> ${pageName}`
    return DomMessageInstance.error({content:message})
  }

  return {
    ...dataInfo,
    data
  }
}

/** 处理从dom中获取到的数据 */
export const dealData = (dataInfo:Partial<OverallQueryDataConfig>) => {
  if(Number(dataInfo?.data?.[0]?.length) < 2) return DomMessageInstance.error({content:'页面数据不完整'})
  const pdateDom = Array.from((document?.querySelector('iframe')?.contentDocument || document)?.querySelectorAll(".fr-trigger-editor"))?.find(val => val.getAttribute('widgetname') === '日期')?.querySelector('.fr-trigger-texteditor') as any
  const pdate = pdateDom?.value
  if(!pdate) DomMessageInstance.error({content:'获取日期失败'})
    // 获取 96 点数据
  const timeArr96 = createSerialDates([pdate, pdate],96,'HH:mm')

  const {cols = [], data = [], keys = [], typeKey = ''} = dataInfo

  const dataSource = timeArr96.map((time, index) => {
    const saveData:Record<string, any> = {
      runDate:`${pdate} ${time}`
    }

    for(let i = 0; i < cols.length; i++) {
      saveData[keys[i]] = data[i][index]
    }

    return saveData
  })

  return {
   dataSource,
   dataType: typeKey,
   date: pdate
  }
}